package com.ascentive.finallyfast.quickboost;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import android.app.ActivityManager.RunningServiceInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.ascentive.finallyfast.R;
import com.ascentive.finallyfast.quickboost.QBProcessInfo.PsRow;
// import com.ascentive.finallyfast.quickboost.QBTaskListAdapters.QBProcessListAdapter.ViewHolder;

public class QBTaskListAdapters {
/*
    public final static class QBTasksListAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        private ArrayList<String> listTasks;
        private ArrayList<String> listServices;
        
        private QuickBoostPage ctx;
        private PackageManager packageManager;

        public QBTasksListAdapter(QuickBoostPage context, List<RunningTaskInfo> list) {
            // Cache the LayoutInflate to avoid asking for a new one each time.
            mInflater = LayoutInflater.from(context);

            // this.listTasks = listTasks;
            // this.listServices = listServices;
            this.ctx = context;
            this.packageManager = ctx.getPackageManager();
        }

        public int getCount() {
            return list.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.list_main, null);

                holder = new ViewHolder();
                holder.icon = (ImageView) convertView.findViewById(R.id.list_icon);
                holder.text_name = (TextView) convertView.findViewById(R.id.list_name);
                holder.text_size = (TextView) convertView.findViewById(R.id.list_size);
                //holder.button = (Button) convertView.findViewById(R.id.selectbutton);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            
            RunningTaskInfo ti = list.get(position);
            convertView.setVisibility(View.VISIBLE);
            String cmd = ti.baseActivity.getPackageName();
            holder.text_name.setText(cmd);
            PsRow row = ctx.getProcessInfo().getPsRow(cmd);
            if (row == null) {
                holder.text_size.setText(R.string.memory_unknown);
            } else {
                holder.text_size.setText((int) Math.ceil(row.mem / 1024) + "K");
            }
            return convertView;
        }

    }
*/
    public final static class QBProcessListAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        // private ArrayList<QBDetailProcess> list;
        private QuickBoostPage ctx;
        private PackageManager pm;

        public final static int TYPE_SECTION_HEADER = 0;
        public final Map<String, Adapter> sections = new LinkedHashMap<String, Adapter>();
        public final ArrayAdapter<String> headers;
        private ArrayList<RunningTaskInfo> listTasks;
        private ArrayList<RunningServiceInfo> listServices;

        public QBProcessListAdapter(QuickBoostPage context) {
        	//, ArrayList<QBDetailProcess> list1, ArrayList<QBDetailProcess> list2) {
            // Cache the LayoutInflate to avoid asking for a new one each time.
            mInflater = LayoutInflater.from(context);
            this.ctx = context;
            this.pm = ctx.getPackageManager();
            headers = new ArrayAdapter<String>(context, R.layout.quick_boost_section_header);
        }
       
        public void addSection(String section, Adapter aAdapter)
		{
        	// ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(this.ctx, R.layout.list_main_text, al);
			this.headers.add(section);
			this.sections.put(section, aAdapter);
		}
        public int getCount() {
            // return list.size(); // tejashree hack
        	// total together all sections, plus one for each section header
			int total = 0;
			for (Adapter adapter : this.sections.values())
				total += adapter.getCount() + 1;
			return total;
        }

        public Object getItem(int position) {
            // return position; //tejashree hack
        	
        	for (Object section : this.sections.keySet())
			{
				Adapter adapter = sections.get(section);
				int size = adapter.getCount() + 1;

				// check if position inside this section
				if (position == 0) return section;
				if (position < size) return adapter.getItem(position - 1);

				// otherwise jump into next section
				position -= size;
			}
        	return null;
        }


		@Override
		public int getViewTypeCount()
			{
				// assume that headers count as one, then total all sections
				int total = 1;
				for (Adapter adapter : this.sections.values())
					total += adapter.getViewTypeCount();
				return total;
			}

		@Override
		public int getItemViewType(int position)
			{
				int type = 1;
				for (Object section : this.sections.keySet())
					{
						Adapter adapter = sections.get(section);
						int size = adapter.getCount() + 1;

						// check if position inside this section
						if (position == 0) return TYPE_SECTION_HEADER;
						if (position < size) return type + adapter.getItemViewType(position - 1);

						// otherwise jump into next section
						position -= size;
						type += adapter.getViewTypeCount();
					}
				return -1;
			}
		@Override
        public long getItemId(int position) {
            return position;
        }
        public boolean areAllItemsSelectable()
		{
			return false;
		}

        @Override
		public boolean isEnabled(int position)
		{
			return (getItemViewType(position) != TYPE_SECTION_HEADER);
		}

        @Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			int sectionnum = 0;
			for (Object section : this.sections.keySet()) {
				System.out.println("sectionnum" + sectionnum);
				Adapter adapter = sections.get(section);
				int size = adapter.getCount() + 1;
				System.out.println("size" + size);
				// check if position inside this section
				if (position == 0)
					return headers.getView(sectionnum, convertView, parent);
				if (position < size)
				// tejashree hack
				{
					if(sectionnum == 0) // Applications
					{
						if (convertView == null) {
							convertView = mInflater.inflate(
									R.layout.list_main, null);

							holder = new ViewHolder();
							 holder.icon = (ImageView) convertView
							 .findViewById(R.id.list_icon);
							holder.text_name = (TextView) convertView
									.findViewById(R.id.list_name);
							 holder.text_size = (TextView) convertView
							 .findViewById(R.id.list_size);
							 holder.button = (Button) convertView
							 .findViewById(R.id.selectbutton);

							convertView.setTag(holder);
						} else {
							holder = (ViewHolder) convertView.getTag();
						}

						 final QBDetailProcess dp = (QBDetailProcess)
						 adapter.getItem(position-1);
						 // list.get(position-1);
						
						 holder.button.setOnClickListener(new OnClickListener() {
						 public void onClick(View v) {
						 // Perform action on clicks
							  MiscUtil.getCloseAppDialog(
							  (QuickBoostPage) v.getContext(), dp).show();
						 }
						 });

						convertView.setVisibility(View.VISIBLE);
						 String cmd = dp.getRuninfo().processName;
						 holder.icon.setImageDrawable(dp.getAppinfo().loadIcon(pm));
						 // holder.icon.setImageDrawable(null);
						 holder.text_name.setText(dp.getTitle());

						 PsRow row = dp.getPsrow();
						 if (row == null) {
						 holder.text_size.setText("Unknown");
						 } else {
						 holder.text_size.setText((int) Math
						 .ceil(row.mem / 1024)
						 + "K");
						 }
						convertView.setOnClickListener(new OnClickListener() {
							@Override
							public void onClick(View view) {
								// MiscUtil.getTaskMenuDialog(ctx, dp).show();
							}
						});
						return convertView;
					}
					else if (sectionnum == 1) // Tasks
					{
					if (convertView == null) {
						convertView = mInflater.inflate(
								R.layout.list_main_text, null);

						holder = new ViewHolder();
						holder.text_name = (TextView) convertView
								.findViewById(R.id.text_name);

						convertView.setTag(holder);
					} else {
						holder = (ViewHolder) convertView.getTag();
					}

					System.out.println("---- section number-------------- "
							+ sectionnum);
					System.out.println("---- position-------------- "
							+ (position - 1));
					System.out.println("---- adapter.getCount()-------------- "
							+ adapter.getCount());

					final QBDetailTask dp = (QBDetailTask) adapter
							.getItem(position - 1); // hack tejashree //

					convertView.setVisibility(View.VISIBLE);
					holder.text_name.setText(dp.getTitleTask());

					convertView.setOnClickListener(new OnClickListener() {
						// @Override
						public void onClick(View view) {
							// MiscUtil.getTaskMenuDialog(ctx, dp).show();
						}
					});
					return convertView;
					} // end of if(sectionnum == 2)
				} // end of if(position < size)
				else {
					// otherwise jump into next section
					position -= size;
					sectionnum++;
				}
			} // end of for()
			// return null;
			return convertView;

		} // end of getView()
  
    } // end of class ProcessListAdapter
    
    private static class ViewHolder {
        ImageView icon;
        TextView text_name;
        TextView text_size;
        Button button;
    }

}

